package gui.mainWindow.finals;

import tournamentTree.TournamentNode;
import tournamentTree.MatchData.Position;

import com.mxgraph.model.mxCell;

import core.Person;
import dataBase.DbHolder;

public class FinalsEntryWindowController {
	private TournamentNode upperNode;
	private TournamentNode lowerNode;
	private final FinalsEntryWindow entryWindow;

	public FinalsEntryWindowController(FinalsEntryWindow entryWindow, mxCell selectedCell) {
		this.entryWindow = entryWindow;
		this.computeInvolvedNodes(selectedCell);
		this.insertPlayerData();
	}

	private void computeInvolvedNodes(mxCell selectedCell) {
		TournamentNode selectedNode = (TournamentNode) selectedCell.getValue();
		if (selectedNode.isUpperChild()) {
			this.upperNode = selectedNode;
			this.lowerNode = selectedNode.getParent().getLowerChild();
		} else {
			this.lowerNode = selectedNode;
			this.upperNode = selectedNode.getParent().getUpperChild();
		}
	}

	private void insertPlayerData() {

		if (this.entryWindow.getMainWindow().getFinalsController().compareRecentScores(this.upperNode, this.lowerNode) >= 0) {
			this.insertLeftPlayerData(this.upperNode);
			this.insertRightPlayerData(this.lowerNode);
		} else {
			this.insertLeftPlayerData(this.lowerNode);
			this.insertRightPlayerData(this.upperNode);
		}
	}

	private void insertLeftPlayerData(TournamentNode node) {
		Person toAdd = DbHolder.getInstance().getArcherData().getById(node.getMatchData().getAssignedPlayerID());
		this.entryWindow.getLeftPlayerName().setText(toAdd.getName());
		this.entryWindow.getLeftPlayerSurName().setText(toAdd.getSurName());
		this.entryWindow.getLeftPlayerClub().setText(toAdd.getClub());

		if (this.upperNode.getMatchData().getPosition() == Position.LEFT) {
			this.entryWindow.getLeftPlayerScoreSpinner().setValue(this.upperNode.getMatchData().getScore());
		} else {
			this.entryWindow.getLeftPlayerScoreSpinner().setValue(this.lowerNode.getMatchData().getScore());
		}

		this.entryWindow.setleftPlayerNode(node);
	}

	private void insertRightPlayerData(TournamentNode node) {
		Person toAdd = DbHolder.getInstance().getArcherData().getById(node.getMatchData().getAssignedPlayerID());
		this.entryWindow.getRightPlayerName().setText(toAdd.getName());
		this.entryWindow.getRightPlayerSurName().setText(toAdd.getSurName());
		this.entryWindow.getRightPlayerClub().setText(toAdd.getClub());

		if (this.upperNode.getMatchData().getPosition() == Position.RIGHT) {
			this.entryWindow.getRightPlayerScoreSpinner().setValue(this.upperNode.getMatchData().getScore());
		} else {
			this.entryWindow.getRightPlayerScoreSpinner().setValue(this.lowerNode.getMatchData().getScore());
		}

		this.entryWindow.setRightPlayerNode(node);
	}

	public void setWinner() {

		int leftPlayerScore = Integer.parseInt(this.entryWindow.getLeftPlayerScoreSpinner().getValue().toString());
		int rightPlayerScore = Integer.parseInt(this.entryWindow.getRightPlayerScoreSpinner().getValue().toString());

		boolean isSemiFinals = this.upperNode.getCurrentDepth() == 2;

		/* left > right */
		if (leftPlayerScore > rightPlayerScore) {
			this.entryWindow.getleftPlayerNode().getMatchData().setScore(leftPlayerScore);
			this.entryWindow.getrightPlayerNode().getMatchData().setScore(rightPlayerScore);
			this.entryWindow.getleftPlayerNode().getParent().getMatchData().setPlayer(this.entryWindow.getleftPlayerNode().getMatchData().getAssignedPlayerID());
			if (isSemiFinals) {
				this.entryWindow.getMainWindow().getFinalsController().insertPlayerToThirdPlaceTree(this.entryWindow.getrightPlayerNode());
			}
		}
		/* right > left */
		else if (leftPlayerScore < rightPlayerScore) {
			this.entryWindow.getleftPlayerNode().getMatchData().setScore(leftPlayerScore);
			this.entryWindow.getrightPlayerNode().getMatchData().setScore(rightPlayerScore);
			this.entryWindow.getrightPlayerNode().getParent().getMatchData().setPlayer(this.entryWindow.getrightPlayerNode().getMatchData().getAssignedPlayerID());
			if (isSemiFinals) {
				this.entryWindow.getMainWindow().getFinalsController().insertPlayerToThirdPlaceTree(this.entryWindow.getleftPlayerNode());
			}
		}
		/* left = right AND left won heads up */
		else if ((leftPlayerScore == rightPlayerScore) && this.entryWindow.getLeftPlayerHeadsUpCheckBox().isSelected()) {
			this.entryWindow.getleftPlayerNode().getMatchData().setScore(leftPlayerScore);
			this.entryWindow.getrightPlayerNode().getMatchData().setScore(rightPlayerScore);
			this.entryWindow.getleftPlayerNode().getParent().getMatchData().setPlayer(this.entryWindow.getleftPlayerNode().getMatchData().getAssignedPlayerID());
			if (isSemiFinals) {
				this.entryWindow.getMainWindow().getFinalsController().insertPlayerToThirdPlaceTree(this.entryWindow.getrightPlayerNode());
			}
		}
		/* left = right AND right won heads up */
		else if ((leftPlayerScore == rightPlayerScore) && this.entryWindow.getRightPlayerHeadsUpCheckBox().isSelected()) {
			this.entryWindow.getleftPlayerNode().getMatchData().setScore(leftPlayerScore);
			this.entryWindow.getrightPlayerNode().getMatchData().setScore(rightPlayerScore);
			this.entryWindow.getrightPlayerNode().getParent().getMatchData().setPlayer(this.entryWindow.getrightPlayerNode().getMatchData().getAssignedPlayerID());
			if (isSemiFinals) {
				this.entryWindow.getMainWindow().getFinalsController().insertPlayerToThirdPlaceTree(this.entryWindow.getleftPlayerNode());
			}
		}

		this.entryWindow.getMainWindow().getFinalsController().checkForPlayability(this.upperNode.getRoot());
		this.entryWindow.getMainWindow().getGraphComponent().getGraphControl().repaint();
	}
}